<?php 
class AppConfig {
	
	var $xml;
	
/*
<apps>
	<default name="irv.set.index"/>
	<app id="account">
		<form name="upload" class="apps.account.AppAccountUploadForm"/>
		
		<entity id="home" class="apps.account.AppAccountHome">
			<action id="index" form="upload" template="ivr_index.tpl">
				<forward name="ok" path="ivr.set"/>
			</action>
		</entity>
		<entity id="image" class="apps.account.AppAccountImage">
			<action id="list" method="showImageListRepository" template="ivr_index.tpl">
				<forward name="ok" path="ivr.set"/>
			</action>
			<action id="upload" template="ivr_index.tpl">
				<forward name="ok" path="ivr.set"/>
			</action>
		</entity>
	</app>
<apps>
*/

	public function __construct($request_url) {
		$this->xml = simplexml_load_file($request_url) or die("file not loading");

		//print_r($this->xml->app);
	}
	
	public function getMessagesResource() {
		return (string)$this->xml->messages["source"];
	}
	
	public function getDefaultApp() {
		return (string)$this->xml->default["name"];
	}
	
	function getApp($appId) {
		foreach ($this->xml->app as $app) {
		    if ((string)$app['id']==$appId) {
		    	return $app;
		    }
		}
	}
	
	function getEntity($appId, $entityId) {
		foreach ($this->xml->app as $app) {
		    if ((string)$app['id']==$appId) {
		    	foreach ($app->entity as $entity) {
		    		if ((string)$entity['id']==$entityId) {
		    			return $entity;
		    		}
		    	}
		    }
		}
	}
	
	function getAction($appId, $entityId, $actionId) {
		//list($appId, $entityId, $actionId) = explode(".", $actionId, 3);
		foreach ($this->xml->app as $app) {
		    if ((string)$app['id']==$appId) {
		    	foreach ($app->entity as $entity) {
		    		if ((string)$entity['id']==$entityId) {
		    			foreach ($entity->action as $action) {
				    		if ((string)$action['id']==$actionId) {
				    			return $action;
				    		}
				    	}
		    		}
		    	}
		    }
		}
	}
	
	
	function getForm($name) {
		if (isset($this->xml->app))
			foreach ($this->xml->app as $app) {
				foreach ($app->form as $form) {
				    if ((string)$form['name']==$name) {
		    			return $form;
				    }
				}
			}
		return null;
	}
	
	function getIBlocks() {
		$result = array("iblocks"=>array(), "isets"=>array());
		if (isset($this->xml->iblocks) && isset($this->xml->iblocks->iblock))
			foreach ($this->xml->iblocks->iblock as $iblock) {
			    $result["iblocks"][(string)$iblock['id']] = array("class"=>(string)$iblock['class']);
			}
		if (isset($this->xml->iblocks) && isset($this->xml->iblocks->iset))
			foreach ($this->xml->iblocks->iset as $iset) {
				foreach ($iset->iblock as $iblock) {
				    $result["isets"][(string)$iset['id']][] = (string)$iblock['name'];
				}
			}
		return $result;
	}
	
	private function addIBSetClasses($setname, $def_iblocks, &$iblocks) {
		if (isset($def_iblocks["isets"][$setname]))
		foreach ($def_iblocks["isets"][$setname] as $ibname) {
			$iblocks["iblocks"][$ibname] = $def_iblocks["iblocks"][$ibname];
		}
	}
	
	private function removeIBSetClasses($setname, $def_iblocks, &$iblocks) {
		if (isset($def_iblocks["isets"][$setname]))
		foreach ($def_iblocks["isets"][$setname] as $ibname) {
			unset($iblocks["iblocks"][$ibname]);
		}
	}
	
	function getEntityIBlocks($appId, $entityId, $def_iblocks) {
		$result = array("iblocks"=>array(), "isets"=>array());
		
		if (isset($this->xml->app)) {
			foreach ($this->xml->app as $app) {
			    if ((string)$app['id']==$appId) {
			    	
			    	if (isset($app["iset"])) {
			    		$this->addIBSetClasses((string)$app["iset"], $def_iblocks, $result);
			    	}
			    	
			    	if (isset($app->entity))
			    	foreach ($app->entity as $entity) {
					    if ((string)$entity['id']==$entityId) {
					    	
					    	if (isset($entity["iset"])) {
			    				$this->addIBSetClasses((string)$entity["iset"], $def_iblocks, $result);
			    			}
					    	
					    	if (isset($entity->iblocks) && isset($entity->iblocks->add) && isset($entity->iblocks->add->iblock))
					    		foreach ($entity->iblocks->add->iblock as $iblock)
							    	$result["iblocks"][(string)$iblock['name']] = $def_iblocks["iblocks"][(string)$iblock['name']];
							if (isset($entity->iblocks) && isset($entity->iblocks->add) && isset($entity->iblocks->add->iset))
					    		foreach ($entity->iblocks->add->iset as $iset)
					    			$this->addIBSetClasses((string)$iset['name'], $def_iblocks, $result);
							
							if (isset($entity->iblocks) && isset($entity->iblocks->remove) && isset($entity->iblocks->remove->iblock))
					    		foreach ($entity->iblocks->remove->iblock as $iblock)
							    	unset($result["iblocks"][(string)$iblock['name']]);
							if (isset($entity->iblocks) && isset($entity->iblocks->remove) && isset($entity->iblocks->remove->iset))
					    		foreach ($entity->iblocks->remove->iset as $iset)
							    	$this->removeIBSetClasses((string)$iset['name'], $def_iblocks, $result);
						}
				    }
				}
		   	}
		}
		
		return $result["iblocks"];
	}
}
?>